using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.Stores;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.Stores
{
    /// <summary>
    /// Store template service
    /// </summary>
    public partial class StoreTemplateService : IStoreTemplateService
    {
        #region Constants
        private const string STORETEMPLATES_BY_ID_KEY = "Nop.storetemplate.id-{0}";
        private const string STORETEMPLATES_ALL_KEY = "Nop.storetemplate.all";
        private const string STORETEMPLATES_PATTERN_KEY = "Nop.storetemplate.";

        #endregion

        #region Fields

        private readonly IRepository<StoreTemplate> _storeTemplateRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;

        #endregion
        
        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="storeTemplateRepository">Store template repository</param>
        /// <param name="eventPublisher">Event published</param>
        public StoreTemplateService(ICacheManager cacheManager,
            IRepository<StoreTemplate> storeTemplateRepository,
            IEventPublisher eventPublisher)
        {
            _cacheManager = cacheManager;
            _storeTemplateRepository = storeTemplateRepository;
            _eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Delete store template
        /// </summary>
        /// <param name="storeTemplate">Store template</param>
        public virtual void DeleteStoreTemplate(StoreTemplate storeTemplate)
        {
            if (storeTemplate == null)
                throw new ArgumentNullException("storeTemplate");

            _storeTemplateRepository.Delete(storeTemplate);

            _cacheManager.RemoveByPattern(STORETEMPLATES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(storeTemplate);
        }

        /// <summary>
        /// Gets all store templates
        /// </summary>
        /// <returns>Store templates</returns>
        public virtual IList<StoreTemplate> GetAllStoreTemplates()
        {
            string key = STORETEMPLATES_ALL_KEY;
            return _cacheManager.Get(key, () =>
            {
                var query = from pt in _storeTemplateRepository.Table
                            orderby pt.DisplayOrder
                            select pt;

                var templates = query.ToList();
                return templates;
            });
        }
 
        /// <summary>
        /// Gets a store template
        /// </summary>
        /// <param name="storeTemplateId">Store template identifier</param>
        /// <returns>Store template</returns>
        public virtual StoreTemplate GetStoreTemplateById(int storeTemplateId)
        {
            if (storeTemplateId == 0)
                return null;

            string key = string.Format(STORETEMPLATES_BY_ID_KEY, storeTemplateId);
            return _cacheManager.Get(key, () =>
            {
                var template = _storeTemplateRepository.GetById(storeTemplateId);
                return template;
            });
        }

        /// <summary>
        /// Inserts store template
        /// </summary>
        /// <param name="storeTemplate">Store template</param>
        public virtual void InsertStoreTemplate(StoreTemplate storeTemplate)
        {
            if (storeTemplate == null)
                throw new ArgumentNullException("storeTemplate");

            _storeTemplateRepository.Insert(storeTemplate);

            //cache
            _cacheManager.RemoveByPattern(STORETEMPLATES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(storeTemplate);
        }

        /// <summary>
        /// Updates the store template
        /// </summary>
        /// <param name="storeTemplate">Store template</param>
        public virtual void UpdateStoreTemplate(StoreTemplate storeTemplate)
        {
            if (storeTemplate == null)
                throw new ArgumentNullException("storeTemplate");

            _storeTemplateRepository.Update(storeTemplate);

            //cache
            _cacheManager.RemoveByPattern(STORETEMPLATES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(storeTemplate);
        }
        
        #endregion
    }
}
